﻿using Nop.Core.Domain.XJ;
using Nop.Services.Common;
using Nop.Services.Customers;
using Nop.Services.Directory;
using Nop.Services.Logging;
using Nop.Services.Media;
using Nop.Services.XJ;
using Nop.Web.Models.Home;
using Nop.Web.Models.XJ;
using System.Linq;
using System.Web.Mvc;

namespace Nop.Web.Controllers
{
	public class StockController : Controller
	{
		#region Fields
		private readonly IDepartmentService _departmentService;
		private readonly ICustomerService _customerService;
		private readonly IFilesService _filesService;
		private readonly IPictureService _pictureService;
		private readonly IMessageService _messageService;
		private readonly ILogger _logger;
		private readonly IGenericAttributeService _genericAttributeService;
		private readonly IXJPlaceService _xjPlaceService;
		private readonly IXJPlaceSignService _xjPlaceSignService;
		private readonly IXJEquipmentCategoryService _xJEquipmentCategoryService;
		private readonly IXJEquipmentService _xJEquipmentService;
		private readonly IXJEquipmentStockService _xJEquipmentStockService;
		private readonly IXJEquipmentStockOutService _xJEquipmentStockOutService;
		private readonly IXJEquipmentStockOutDetailService _xJEquipmentStockOutDetailService;
		private readonly IXJEquipmentStockInService _xJEquipmentStockInService;
		private readonly IXJEquipmentStockInDetailService _xJEquipmentStockInDetailService;
		private readonly IXJEquipmentStockLogService _xJEquipmentStockLogService;
		private readonly IXJInspectionService _xJInspectionService;
		private readonly IXJInspectionPlanService _xJInspectionPlanService;
		private readonly IXJMaintenanceService _xJMaintenanceService;
		private readonly IXJMaintenanceCommentService _xJMaintenanceCommentService;
		#endregion

		#region Ctor
		public StockController(
			IDepartmentService departmentService,
			ICustomerService customerService,
			IFilesService filesService,
			IPictureService pictureService,
			IMessageService messageService,
			ILogger logger,
			IGenericAttributeService genericAttributeService,
			IXJPlaceService xjPlaceService,
			IXJPlaceSignService xjPlaceSignService,
			IXJEquipmentCategoryService xJEquipmentCategoryService,
			IXJEquipmentService xJEquipmentService,
			IXJEquipmentStockService xJEquipmentStockService,
			IXJEquipmentStockOutService xJEquipmentStockOutService,
			IXJEquipmentStockOutDetailService xJEquipmentStockOutDetailService,
			IXJEquipmentStockInService xJEquipmentStockInService,
			IXJEquipmentStockInDetailService xJEquipmentStockInDetailService,
			IXJEquipmentStockLogService xJEquipmentStockLogService,
			IXJInspectionService xJInspectionService,
			IXJInspectionPlanService xJInspectionPlanService,
			IXJMaintenanceService xJMaintenanceService,
			IXJMaintenanceCommentService xJMaintenanceCommentService)
		{
			this._departmentService = departmentService;
			this._customerService = customerService;
			this._filesService = filesService;
			this._pictureService = pictureService;
			this._messageService = messageService;
			this._logger = logger;
			this._genericAttributeService = genericAttributeService;
			this._xjPlaceService = xjPlaceService;
			this._xjPlaceSignService = xjPlaceSignService;
			this._xJEquipmentCategoryService = xJEquipmentCategoryService;
			this._xJEquipmentService = xJEquipmentService;
			this._xJEquipmentStockService = xJEquipmentStockService;
			this._xJEquipmentStockOutService = xJEquipmentStockOutService;
			this._xJEquipmentStockOutDetailService = xJEquipmentStockOutDetailService;
			this._xJEquipmentStockInService = xJEquipmentStockInService;
			this._xJEquipmentStockInDetailService = xJEquipmentStockInDetailService;
			this._xJEquipmentStockLogService = xJEquipmentStockLogService;
			this._xJInspectionService = xJInspectionService;
			this._xJInspectionPlanService = xJInspectionPlanService;
			this._xJMaintenanceService = xJMaintenanceService;
			this._xJMaintenanceCommentService = xJMaintenanceCommentService;
		}
		#endregion

		#region Utilities

		#endregion

		[HttpPost]
		public JsonResult Filter()
		{
			var model = new StockOutFilterModel();
			var categorys = _xJEquipmentCategoryService.GetAllXJEquipmentCategorys().Where(m => m.IsConsumables);
			model.AvailableCategories = categorys.Select(x => { return new SelectItemModel { Id = x.Id, Text = x.Name, PId = x.PId }; }).ToList();
			return Json(new
			{
				result = true,
				model
			});
		}

		[HttpPost]
		public JsonResult List(int categoryId = 0, int placeId = 0, int partnerId = 0, string name = "", string pp = "", string gg = "", string xh = "", string location = "", bool onlyStock = false, int pageIndex = 0, int pageSize = 10)
		{
			var stocks = _xJEquipmentStockService.GetAllStocks(
				categoryId: categoryId,
				placeId: placeId,
				partnerId: partnerId,
				//keyword: name,
				name: name,
				pp: pp,
				gg: gg,
				xh: xh,
				location: location,
				onlyStock: onlyStock,
				state: (int)StockStateEnum.正常,
				sort: "Name",
				pageIndex: pageIndex,
				pageSize: pageSize);
			return Json(new
			{
				result = true,
				model = stocks.Select(x =>
				{
					return x;
				}),
				stocks.PageIndex,
				stocks.TotalPages
			});
		}
	}
}